package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Goods;
import pers.cj.pdos.entity.OrderRule;
import pers.cj.pdos.service.IOrderRuleService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.vo.OrderRuleVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 核销单编号，商品编号，借出单，进货订单编号，商品模板，入库单编号，销售订单编号，销售编号,调拨单,商品套餐,借出归还单 前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/sett")
public class OrderRuleController extends BaseController {
    @Autowired
    private IOrderRuleService orderRuleService;

    @RequiresPermissions("orderRule:query")
    @RequestMapping("/orderRule")
    public String orderRule(){
        return "sett/orderRule";
    }


    @RequiresPermissions("orderRule:insert")
    @RequestMapping("/orderRuleEdit")
    public String orderRuleEdit(String id, Model model){
        model.addAttribute("id",id);
        return "sett/orderRuleEdit";
    }

    @ResponseBody
    @RequiresPermissions("orderRule:query")
    @RequestMapping("/orderRule/getOrderRules")
    public ResponseList getOrderRules(int page, int limit) {
        Company company = getCompany();

        List<OrderRule> list = orderRuleService.listPage(page, limit,company.getId());
        List<OrderRuleVo> queryHouseList = new ArrayList<>();
        for (OrderRule orderRule : list) {
            OrderRuleVo vo = DataUtil.genVoFromEntity(OrderRuleVo.class, orderRule);
            vo.setName(OrderType.getObj(orderRule.getType()).getName());
            queryHouseList.add(vo);
        }

        //返回数据
        return ResponseList.success(orderRuleService.count(company.getId()), queryHouseList);
    }

    @ResponseBody
    @RequiresPermissions("orderRule:insert")
    @RequestMapping("/orderRule/update")
    public ResponseData update(@RequestBody OrderRule orderRule) {
        DataUtil.processEntity(false, getCompany(), orderRule);
        orderRuleService.update(orderRule);
        OrderRuleVo vo = DataUtil.genVoFromEntity(OrderRuleVo.class, orderRule);
        vo.setName(OrderType.getObj(orderRule.getType()).getName());
        return ResponseData.success(vo);
    }


    /**
     * 当单号规则，超过最大限制时，则位数失效，直接用真实当前值。
     * 单号规则里面的位数本来是3位，最大是到999. 商品的数量新增数量为1000，则直接使用1000
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/orderRule/getByType")
    public ResponseData getByType(int type) {
        Company company = getCompany();

        OrderRule orderRule = orderRuleService.getByType(company.getId(),type);
        return ResponseData.success(orderRule);
    }

    @ResponseBody
    @RequestMapping("/orderRule/getById")
    public ResponseData getById(String id) {
        OrderRule orderRule = orderRuleService.getById(id);
        if (orderRule==null){
            return ResponseData.fail("未获取到数据");
        }
        OrderRuleVo vo = DataUtil.genVoFromEntity(OrderRuleVo.class, orderRule);
        vo.setName(OrderType.getObj(orderRule.getType()).getName());
        return ResponseData.success(vo);
    }
}
